﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.Plan
{
    public partial class MatRequirementView : AuthBasePage
    {
        public DataTable MaterialRequirementDetailDT
        {
            set { Session["MaterialRequirementDetailDT"] = value; }
            get
            {
                if (Session["MaterialRequirementDetailDT"] == null)
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["MaterialRequirementDetailDT"];
                }
            }
        }
        public DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MaterialRequirementDetailID");
            dt.Columns.Add("PlanDetailID");
            dt.Columns.Add("IsDirect");
            dt.Columns.Add("MaterialID");
            dt.Columns.Add("MaterialName");
            dt.Columns.Add("Specifications");
            dt.Columns.Add("Unit");
            dt.Columns.Add("MMNumber");//单件毫米数
            dt.Columns.Add("MeterWeight");//每米重量
            dt.Columns.Add("TotalNum");//总需求量
            dt.Columns.Add("StockNum");//库存数量
            dt.Columns.Add("OnWayNum");//在途数量
            dt.Columns.Add("MinOrderWeight");//最小送货量
            dt.Columns.Add("AdviseNum");//建议请购数量（毫米数）
            dt.Columns.Add("PieceNum");//件数
            dt.Columns.Add("Weight");//重量
            dt.Columns.Add("IsNew");
            dt.Columns.Add("IsDel");
            return dt;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MaterialRequirementDetailDT = null;
                InitData();
            }
            Master.ValidationGroup = "gSubmit";
        }

        private void InitData()
        {
            string id = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id"));
            if (!string.IsNullOrEmpty(id))
            {
                DataTable dt = BsMaterialRequirementBusiness.GetDataTablebByID(id);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    this.lblMaterialRequirementID.Text = CommonMethod.GetString(dr["MaterialRequirementID"]);
                    this.lblBillDate.Text = CommonMethod.GetString(dr["BillDates"]);
                    this.txtPlanID.Text = CommonMethod.GetString(dr["PlanID"]);
                    this.lblRemark.Text = CommonMethod.GetString(dr["Remark"]);

                    DataTable dtemp = MaterialRequirementDetailDT.Copy();
                    foreach (DataRow drow in dt.Rows)
                    {
                        DataRow d = dtemp.NewRow();
                        d["MaterialRequirementDetailID"] = CommonMethod.GetString(drow["MaterialRequirementDetailID"]);
                        d["PlanDetailID"] = CommonMethod.GetString(drow["PlanDetailID"]);
                        d["IsDirect"] = CommonMethod.GetString(drow["IsDirect"]);
                        d["MaterialID"] = CommonMethod.GetString(drow["MaterialID"]);
                        d["MaterialName"] = CommonMethod.GetString(drow["MaterialName"]);
                        d["Specifications"] = CommonMethod.GetString(drow["Specifications"]);
                        d["Unit"] = CommonMethod.GetString(drow["Unit"]);
                        d["MMNumber"] = CommonMethod.GetString(drow["MMNumber"]);
                        d["MeterWeight"] = CommonMethod.GetString(drow["MeterWeight"]);
                        d["TotalNum"] = CommonMethod.GetString(drow["TotalNum"]);
                        d["StockNum"] = CommonMethod.GetString(drow["StockNum"]);
                        d["OnWayNum"] = CommonMethod.GetString(drow["OnWayNum"]);
                        d["MinOrderWeight"] = CommonMethod.GetString(drow["MinOrderWeight"]);
                        d["AdviseNum"] = CommonMethod.GetString(drow["AdviseNum"]);
                        d["PieceNum"] = CommonMethod.GetString(drow["PieceNum"]);
                        d["Weight"] = CommonMethod.GetString(drow["Weight"]);
                        d["IsNew"] = "-1";
                        d["IsDel"] = "-1";
                        dtemp.Rows.Add(d);
                    }
                    MaterialRequirementDetailDT = dtemp.Copy();
                }
            }
            BindGridView();
        }

        private void BindGridView()
        {
            DataView dv = new DataView(MaterialRequirementDetailDT);
            dv.RowFilter = " IsDel <> '1'";
            //dv.Sort = "BOMDetailID ";
            DataTable dt = dv.ToTable();
            if (dt != null && dt.Rows.Count > 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.UpdatePanel4, "iframeAutoFit();");
        }
    }
}